﻿package net.wangit.dev.version;

import net.wangit.context.LocalContext;
import net.wangit.dev.ConsoleDomainUtil;
import net.wangit.dev.component.ChartTree;
import net.wangit.dev.component.DataPrivilegeTree;
import net.wangit.dev.component.EnumerationTree;
import net.wangit.dev.component.IncrementorTree;
import net.wangit.dev.component.ObjectTree;
import net.wangit.dev.component.TableTree;
import net.wangit.dev.component.TreeTree;
import net.wangit.dev.component.UnitTree;
import net.wangit.jdbc.util.DBHelper;
import net.wangit.support.TreeFactory;
import net.wangit.view.tree.Tree;

public class ObjectTreeFactory extends TreeFactory {

	private static final String ROOT_ICON = "/domain/dev/c/component.png";

	public ObjectTreeFactory() {
	}

	public Tree createTree(String treeDomain) throws Exception {
		net.wangit.context.ActionContext ac = LocalContext.getLocalContext()
				.getActionContext();
		String domain = ConsoleDomainUtil.getAndSetDomain();
		net.wangit.jdbc.DB db = DBHelper.getDomainDB(domain);
		Tree root = new Tree();
		root.setId("_ot_");
		root.setIcon(ObjectTreeFactory.ROOT_ICON);
		root.setCheckValue("-");
		ObjectTree ot = getObjectTree(root);
		ot.setCheck(true);
		int count[] = ot.addTree(ac, db, root);
		root.setName((new StringBuilder(String.valueOf(root.getName())))
				.append(" ").append(count[0]).toString());
		return root;
	}

	private static ObjectTree getObjectTree(Tree root) {
		int type = Integer.parseInt(OutputUI.getComponentType());
		switch (type) {
		case 1:
			root.setName("表");
			return new TableTree();

		case 2:
			root.setName("单元");
			return new UnitTree();

		case 3:
			root.setName("枚举");
			return new EnumerationTree();

		case 4:
			root.setName("树");
			return new TreeTree();

		case 5:
			root.setName("统计图");
			return new ChartTree();

		case 6:
			root.setName("数据权限");
			return new DataPrivilegeTree();

		case 7:
			root.setName("自增器");
			return new IncrementorTree();
		}
		return null;
	}
}
